package com.kingdee.eas.custom.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailCollection;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailFactory;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementInfo;

public class CanonFacadeControllerBean extends
		AbstractCanonFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.common.CanonFacadeControllerBean");

	@Override
	protected void _scheduleGenByYear(Context ctx) throws BOSException,
			EASBizException {

		int year = 2013;
		this._scheduleGenByYear(ctx, String.valueOf(year));
	}

	@Override
	protected void _scheduleGenByYear(Context ctx, String year)
			throws BOSException, EASBizException {

		EntityViewInfo view = new EntityViewInfo();
		view.getSelector().add(new SelectorItemInfo("id"));
		view.getSelector().add(new SelectorItemInfo("workforceManagement.id"));
		view.getSelector().add(
				new SelectorItemInfo("workforceManagement.number"));
		view.getSelector().add(new SelectorItemInfo("workGroup"));

		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("workforceManagement.id", null,
						CompareType.ISNOT));
		fi.getFilterItems().add(
				new FilterItemInfo("workGroup", null, CompareType.ISNOT));

		PersonCollection collection = PersonFactory.getLocalInstance(ctx)
				.getPersonCollection(view);

		Map<WorkforceManagementInfo, Map<String, List<PersonInfo>>> wfmMap = new HashMap<WorkforceManagementInfo, Map<String, List<PersonInfo>>>();

		Map<String, List<PersonInfo>> groupMap = null;

		List<PersonInfo> pers = new ArrayList<PersonInfo>();

		PersonInfo person = null;

		Set<String> wfmNumSet = new HashSet<String>();

		for (int i = 0; i < collection.size(); i++) {
			person = collection.get(i);
			if (person.getWorkforceManagement() != null) {

				WorkforceManagementInfo wfm = person.getWorkforceManagement();
				String grp = person.getWorkGroup();

				if (wfmMap.containsKey(wfm)) {
					groupMap = wfmMap.get(wfm);
				} else {
					groupMap = new HashMap<String, List<PersonInfo>>();
					wfmMap.put(wfm, groupMap);
					wfmNumSet.add(wfm.getNumber());
				}

				if (groupMap.containsKey(grp)) {
					pers = groupMap.get(grp);
				} else {
					pers = new ArrayList<PersonInfo>();
					groupMap.put(grp, pers);
				}
				pers.add(person);
			}
		}
		/*********************************************************************************************/

		view = new EntityViewInfo();

		view.getSelector().add(new SelectorItemInfo("workforceManagement.id"));
		view.getSelector().add(
				new SelectorItemInfo("workforceManagement.number"));
		view.getSelector().add(new SelectorItemInfo("*"));

		fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("workforceManagement.applyYear", year));

		SchedulDetailCollection schedulDetailCollection = SchedulDetailFactory
				.getLocalInstance(ctx).getSchedulDetailCollection(view);

		
		/*********************************************************************************************/
		
		
		
		Set<Entry<WorkforceManagementInfo, Map<String, List<PersonInfo>>>> entrySet = wfmMap
				.entrySet();

		Iterator<Entry<WorkforceManagementInfo, Map<String, List<PersonInfo>>>> it = entrySet
				.iterator();

		Entry<WorkforceManagementInfo, Map<String, List<PersonInfo>>> entry = null;
		WorkforceManagementInfo wfm = null;

		Iterator<Entry<String, List<PersonInfo>>> grpIt = null;
		while (it.hasNext()) {
			entry = it.next();
			wfm = entry.getKey();

			groupMap = entry.getValue();
			grpIt = groupMap.entrySet().iterator();

			while (grpIt.hasNext()) {
				
			}
		}
	}
}